package com.henry.Client;

import com.henry.model.Question;
import com.henry.model.User;
import com.henry.model.UserMessage;

import java.io.IOException;
import java.util.List;
import java.util.Scanner;

public class ClientManager {
    private ClientInitClose cic;
    private Scanner sc;

    public ClientManager() {
    }

    public ClientManager(ClientInitClose cic) {
        this.cic = cic;
    }

    public void clientManagerLogin() throws IOException, ClassNotFoundException {
        sc = ClientScanner.getScanner();
        System.out.println("请输入管理员账户：");
        String userName = sc.next();
        System.out.println("请输入管理员密码：");
        String passWord = sc.next();
        UserMessage um = new UserMessage("managerCheck",new User(userName, passWord));
        cic.getOos().writeObject(um);

        UserMessage userMessage = (UserMessage) cic.getOis().readObject();
        System.out.println(userMessage.getType());
        if(userMessage.getType().equals("success")) {
            while (true) {
                System.out.println("[1] 学员管理模块  [2] 考题管理模块\n[3] 导出成绩  [4] 退出登陆");
                System.out.println("请选择：");
                int i = sc.nextInt();
                if (4 == i) {
                    cic.getOos().writeObject(new UserMessage("end", new User()));
                    return;
                } else if (1 == i) {
                    while (true) {
                        System.out.println("\t学员管理模块");
                        System.out.println("[1] 增加学员 [2] 删除学员\n[3] 修改学员  [4] 查找学员\n[5] 返回上级目录");
                        System.out.println("请选择：");
                        int i1 = sc.nextInt();
                        if (5 == i1)
                            break;
                        switch (i1) {
                            case 1:
                                addStudent("新增", "addStudent");
                                break;
                            case 2:
                                deleteStudent("要删除的", "deleteStudent");
                                break;
                            case 3:
                                reviseStudent("要修改的", "reviseStudent");
                                break;
                            case 4:
                                findStudent("要查找的", "findStudent");
                                break;
                            default:
                        }
                    }
                } else if (2 == i) {
                    ClientDao cd = ClientDao.getCd();
                    while (true) {
                        System.out.println("\t考题管理模块");
                        System.out.println("[1] 增加考题 [2] 删除考题\n[3] 修改考题  [4] 查找考题\n[5] 导入考题  [6] 返回上级目录");
                        System.out.println("请选择：");
                        int i2 = sc.nextInt();
                        if (6 == i2)
                            break;
                        switch (i2) {
                            case 1:
                                addQuestion(cd);
                                break;
                            case 2:
                                deleteQuestion(cd);
                                break;
                            case 3:
                                reviseQuestion(cd);
                                break;
                            case 4:
                                findQuestion(cd);
                                break;
                            case 5:
                                inputQuestion(cd);
                            default:
                        }
                    }
                }else if(3 == i) {
                    cic.getOos().writeObject(new UserMessage("outputGrade"));
                }
            }
        }
    }

    private void addStudent(String str,String str1) throws IOException, ClassNotFoundException {
        System.out.println("请输入" + str + "学员信息：(账号、密码、姓名、性别、年龄、班级、状态)");
        String[] s = new String[7];
        for (int i = 0; i < 7; i++) {
            s[i] = sc.next();
        }
        UserMessage us = new UserMessage(str1, new User(s[0],s[1],s[2],s[3],s[4],s[5],s[6]));
        cic.getOos().writeObject(us);
        printResult();
    }
    private void deleteStudent(String str, String str1) throws IOException, ClassNotFoundException {
        System.out.println("请输入" + str + "学员账号：");
        String s = sc.next();
        User user = new User();
        user.setUserName(s);
        UserMessage us = new UserMessage(str1, user);
        cic.getOos().writeObject(us);
        printResult();
    }
    private void reviseStudent(String str, String str1) throws IOException, ClassNotFoundException {
        addStudent(str, str1);
    }
    private void findStudent(String str, String str1) throws IOException, ClassNotFoundException {
        System.out.println("请输入" + str + "学员账号：");
        String s = sc.next();
        User user = new User();
        user.setUserName(s);
        UserMessage us = new UserMessage(str1, user);
        cic.getOos().writeObject(us);

        UserMessage userMessage = (UserMessage)cic.getOis().readObject();
        System.out.println(userMessage.toString());
        /*if(us.getType().equals("true")) {
        }else {
            System.out.println("查找失败！");
        }*/
    }
    private void printResult() throws IOException, ClassNotFoundException {
        UserMessage userMessage = (UserMessage) cic.getOis().readObject();
        System.out.println(userMessage.getType());
    }

    private void addQuestion(ClientDao cd) {
        System.out.println("请输入答案和题目描述：");
        String ans = sc.next();
        String des = sc.next();
        System.out.println("请输入选项：");
        String opt1 = sc.next();
        String opt2 = sc.next();
        String opt3 = sc.next();
        String opt4 = sc.next();
        boolean b = cd.addQuestion(new Question(ans, des, opt1, opt2, opt3, opt4));
        System.out.println("添加题目" + (b ? "成功！" : "失败！"));
    }

    private void deleteQuestion(ClientDao cd) {
        System.out.println("请输入需要删除的题目序号：");
        boolean b = cd.deleteQuestion(sc.nextInt());
        System.out.println("删除题目" + (b ? "成功！" : "失败！"));
    }

    private void reviseQuestion(ClientDao cd) {
        System.out.println("请输入需要修改的题目序号：");
        Question q = new Question();
        q.setNumber(sc.nextInt());
        System.out.println("请输入需要修改的部分：答案 题目描述 选项1 选项2 选项3 选项(不改部分输入-1)");
        q.setAnswer(sc.next());
        q.setDescription(sc.next());
        q.setOptionA(sc.next());
        q.setOptionB(sc.next());
        q.setOptionC(sc.next());
        q.setOptionD(sc.next());
        boolean b = cd.reviseQuestion(q);
        System.out.println("修改题目" + (b ? "成功！" : "失败！"));
    }

    private void findQuestion(ClientDao cd) {
        System.out.println("请输入题目关键字：");
        String next = sc.next();
        List<Question> questions = cd.findQuestion(next);
        for (Question q : questions) {
            System.out.println(q.toString());
        }
    }

    private void inputQuestion(ClientDao cd) {
        System.out.println("请输入要导入的题目库路径：(例：e:/question.txt)");
        boolean b = cd.inputQuestion(sc.next());
        System.out.println("导入题目" + (b ? "成功！" : "失败！"));
    }
}
